/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*   File:      AGV_Send_TaskDialog.cpp
*   Author:    刘洋兵
*   Function:  实现AGV/笼车任务下发
*   Time:      2025-09-09
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#include "AGV_Send_TaskDialog.h"
#include "ui_AGV_Send_TaskDialog.h"

AGV_Send_TaskDialog::AGV_Send_TaskDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::AGV_Send_TaskDialog),
    isSpecificVehicle(false)
{
    ui->setupUi(this);
    connect(ui->comboBox_action_group, QOverload<const QString &>::of(&QComboBox::currentTextChanged),
            this, &AGV_Send_TaskDialog::comboBoxData);

    connect(ui->comboBox_action_group_transfer_trolley, QOverload<const QString &>::of(&QComboBox::currentTextChanged),
            this, &AGV_Send_TaskDialog::comboBoxData);

    connect(ui->comboBox_map_task, QOverload<const QString &>::of(&QComboBox::currentTextChanged),
            this, &AGV_Send_TaskDialog::maptoBoxData);

    connect(ui->comboBox_start_district_type_s, QOverload<const QString &>::of(&QComboBox::currentTextChanged),
            this, &AGV_Send_TaskDialog::districtNametoBoxData);

    connect(ui->comboBox_start_district_type, QOverload<const QString &>::of(&QComboBox::currentTextChanged),
            this, &AGV_Send_TaskDialog::districtNametoBoxData);

    connect(ui->comboBox_end_district_type, QOverload<const QString &>::of(&QComboBox::currentTextChanged),
            this, &AGV_Send_TaskDialog::districtNametoBoxData);

    connect(ui->comboBox_start_district_name_s, QOverload<const QString &>::of(&QComboBox::currentTextChanged),
            this, &AGV_Send_TaskDialog::seattoBoxData);

    connect(ui->comboBox_start_district_name, QOverload<const QString &>::of(&QComboBox::currentTextChanged),
            this, &AGV_Send_TaskDialog::seattoBoxData);

    connect(ui->comboBox_end_district_name, QOverload<const QString &>::of(&QComboBox::currentTextChanged),
            this, &AGV_Send_TaskDialog::seattoBoxData);

    switchBtn = new SwitchButton(this);
    QObject::connect(switchBtn, &SwitchButton::toggled, [&](bool checked) {
        if(checked)
        {
            ui->label_6->setHidden(false);
            ui->comboBox_vehicle->setHidden(false);
            isSpecificVehicle = true;
        }else {
            ui->label_6->setHidden(true);
            ui->comboBox_vehicle->setHidden(true);
            isSpecificVehicle = false;
        }
    });

    ui->Layout_vehicle->addWidget(switchBtn, 0, Qt::AlignLeft);
    ui->Layout_vehicle->setContentsMargins(0,0,0,0);

}

AGV_Send_TaskDialog::~AGV_Send_TaskDialog()
{
    delete ui;
}

/**
 * @brief  初始化comboBox控件内容
 */
void AGV_Send_TaskDialog::comboBoxData(const QString& text)
{
    if(text.isEmpty())
    {
        return;
    }
    QComboBox *comboBox = qobject_cast<QComboBox*>(sender());
    if(!comboBox)
    {
        return;
    }
    QString boxName = comboBox->objectName();
    QString condition = "action_group_id";
    QString group_id = comboBox->currentData().toString();
    QString sql_group = "start_action_code,end_action_code";
    QString sql = QString("select %1 from tb_action_group where %2 = %3").arg(sql_group).arg(condition).arg(group_id);
    QList<QStringList> dataList_group = MyDatabase::getInstance().query_table(sql);
    if(dataList_group.isEmpty())
    {
        return;
    }
    int start_action_code = dataList_group[0][0].toInt();
    int end_action_code = dataList_group[0][1].toInt();
    if(start_action_code > 1 && end_action_code > 1)
    {
        if(boxName == "comboBox_action_group")
        {
            ui->widget_start->setHidden(false);
            ui->widget_end->setHidden(false);
        }else {
            ui->widget_start_s->setHidden(false);
        }
    }
    else if(start_action_code == 1 && end_action_code > 1)
    {
        if(boxName == "comboBox_action_group")
        {
            ui->widget_start->setHidden(false);
            ui->widget_end->setHidden(true);
        }else {
            ui->widget_start_s->setHidden(false);
        }
    }
    else if(start_action_code > 1 && end_action_code == 1)
    {
        if(boxName == "comboBox_action_group")
        {
            ui->widget_start->setHidden(true);
            ui->widget_end->setHidden(true);
        }else {
            ui->widget_start_s->setHidden(true);
        }
    }
    else if(start_action_code == 1 && end_action_code == 1)
    {
        if(boxName == "comboBox_action_group")
        {
            ui->widget_start->setHidden(false);
            ui->widget_end->setHidden(true);
        }else {
            ui->widget_start_s->setHidden(false);
        }
    }
}

/**
 * @brief  显示指定窗口
 * @param  index 窗口索引
 */
void AGV_Send_TaskDialog::showDialog(const int& index,const int& id)
{
    static bool is_first = true;
    ui->stackedWidget->setCurrentIndex(index);
    District_Seat D_seat;

    QString sql_map = "map_code";
    QString dbtablename_map = "tb_district_seats";
    QList<QStringList> dataList_map = MyDatabase::getInstance().query_some_tabledata(sql_map,dbtablename_map);
    int rowcount_map = dataList_map.count();

    QString sql_group = "action_group_id,action_group_name";
    QString dbtablename_group = "tb_action_group";
    QList<QStringList> dataList_group = MyDatabase::getInstance().query_some_tabledata(sql_group,dbtablename_group);
    int rowcount_group = dataList_group.count();

    if(index == 0)
    {
        setWindowTitle(QStringLiteral("任务替换"));

        VehicleDataType Vtype;
        ui->groupBox_end_s->setHidden(true);

        ui->comboBox_source_task_id->clear();
        ui->comboBox_transfer_trolley_id->clear();
        ui->comboBox_action_group_transfer_trolley->blockSignals(true);
        ui->comboBox_action_group_transfer_trolley->clear();
        ui->comboBox_action_group_transfer_trolley->blockSignals(false);

        QString sql = "source_task_id,transfer_trolley_id,map_code";
        QString dbtablename = "tb_tasks";
        int taskId = id;
        QString id = QString("task_id = %1").arg(taskId);
        QList<QStringList> dataList = MyDatabase::getInstance().query_some_tabledata(sql,dbtablename,id);
        ui->comboBox_source_task_id->addItem(dataList[0][0]);
        ui->comboBox_transfer_trolley_id->addItem(dataList[0][1]);
        my_map_code = dataList[0][2];

        QString sql_seat = "district_type";
        QString dbtablename_seat = "tb_district_seats";
        QString map_code = QString("map_code = '%1'").arg(my_map_code);
        QList<QStringList> dataList_seat = MyDatabase::getInstance().query_some_tabledata(sql_seat,dbtablename_seat,map_code);
        int rowcount_seat = dataList_seat.count();

        for (int row = 0; row < rowcount_group; row++) {
            ui->comboBox_action_group_transfer_trolley->addItem(dataList_group[row][1],dataList_group[row][0]);
        }

        for (int row = 0; row < rowcount_seat; row++) {
            int typenumber = dataList_seat[row][0].toInt();
            QString type = Vtype.District_Seats["区域类型"][dataList_seat[row][0].toInt()];
            if(ui->comboBox_start_district_type_s->findText(type)==-1)
            {
                ui->comboBox_start_district_type_s->addItem(type,typenumber);
                ui->comboBox_end_district_type_s->addItem(type,typenumber);
            }
        }

        if(is_first)
        {
            ui->comboBox_priority_s->addItem("优先级5",5);
            ui->comboBox_priority_s->addItem("优先级4",4);
            ui->comboBox_priority_s->addItem("优先级3",3);
            ui->comboBox_priority_s->addItem("优先级2",2);
            ui->comboBox_priority_s->addItem("优先级1",1);
            is_first = false;
        }

    }
    else if(index == 1)
    {
        setWindowTitle(QStringLiteral("AGV小车任务下发"));
        QString sql_vehicle = "vehicle_id,vehicle_name,is_available";
        QString dbtablename_vehicle = "tb_vehicle_profile";
        QList<QStringList> dataList_vehicle = MyDatabase::getInstance().query_some_tabledata(sql_vehicle,dbtablename_vehicle);
        int rowcount_vehicle = dataList_vehicle.count();
        ui->comboBox_map_task->blockSignals(true);
        ui->comboBox_map_task->clear();
        ui->comboBox_map_task->blockSignals(false);
        ui->comboBox_vehicle->clear();
        ui->comboBox_action_group->blockSignals(true);  // 阻塞ui->comboBox_action_group信号发送
        ui->comboBox_action_group->clear();
        ui->comboBox_action_group->blockSignals(false);
        for (int row = 0; row < rowcount_map; row++) {
            QString map_id = QString("map_code = '%1'").arg(dataList_map[row][0]);
            QString sql = "map_name";
            QString dbtablename = "tb_map_config";
            QList<QStringList> dataList = MyDatabase::getInstance().query_some_tabledata(sql,dbtablename,map_id);
            if(ui->comboBox_map_task->findText(dataList[0][0]) == -1)
            {
                ui->comboBox_map_task->addItem(dataList[0][0],dataList_map[row][0]);
            }
        }
        for (int row = 0; row < rowcount_vehicle; row++) {
            int is_use = dataList_vehicle[row][2].toInt();
            if(is_use)
            {
                ui->comboBox_vehicle->addItem(dataList_vehicle[row][1],dataList_vehicle[row][0]);
            }
        }
        for (int row = 0; row < rowcount_group; row++) {
            ui->comboBox_action_group->addItem(dataList_group[row][1],dataList_group[row][0]);
        }

        if(is_first)
        {
            ui->comboBox_priority->addItem("优先级5",5);
            ui->comboBox_priority->addItem("优先级4",4);
            ui->comboBox_priority->addItem("优先级3",3);
            ui->comboBox_priority->addItem("优先级2",2);
            ui->comboBox_priority->addItem("优先级1",1);
            is_first = false;
        }
        ui->widget_end->setHidden(true);
        // 小车label和下拉框
        ui->label_6->setHidden(true);
        ui->comboBox_vehicle->setHidden(true);

    }
}

/**
 * @brief  指定地图显示对应的下拉框选项
 */
void AGV_Send_TaskDialog::maptoBoxData(const QString& text)
{
    if(text.isEmpty())
    {
        return;
    }
    QString sql_seat = "district_type";
    QString dbtablename_seat = "tb_district_seats";

    QComboBox *comboBox = qobject_cast<QComboBox*>(sender());
    if(!comboBox)
    {
        return;
    }
    QString boxName = comboBox->objectName();
    QString idString = comboBox->currentData().toString();
    QString id = QString("map_code = '%1'").arg(idString);
    QList<QStringList> dataList_seat = MyDatabase::getInstance().query_some_tabledata(sql_seat,dbtablename_seat,id);
    if(dataList_seat.isEmpty())
    {
        return;
    }
    VehicleDataType Vtype;
    int rowcount_seat = dataList_seat.count();
    {
        ui->comboBox_start_district_type->blockSignals(true);
        ui->comboBox_start_district_type->clear();
        ui->comboBox_start_district_type->blockSignals(false);
        ui->comboBox_end_district_type->blockSignals(true);
        ui->comboBox_end_district_type->clear();
        ui->comboBox_end_district_type->blockSignals(false);

        for (int row = 0; row < rowcount_seat; row++) {
            int typenumber = dataList_seat[row][0].toInt();
            QString type = Vtype.District_Seats["区域类型"][dataList_seat[row][0].toInt()];
            if(ui->comboBox_start_district_type->findText(type)==-1)
            {
                ui->comboBox_start_district_type->addItem(type,typenumber);
                ui->comboBox_end_district_type->addItem(type,typenumber);
            }
        }
    }

}


/**
 * @brief  指定区域名下拉框选项
 */
void AGV_Send_TaskDialog::districtNametoBoxData(const QString& text)
{
    if(text.isEmpty())
    {
        return;
    }
    QComboBox *comboBox = qobject_cast<QComboBox*>(sender());
    if(!comboBox)
    {
        return;
    }
    QString boxName = comboBox->objectName();
    QString type = comboBox->currentData().toString();
    QString map_code = ui->comboBox_map_task->currentData().toString();
    if(map_code.isEmpty())
    {
        map_code = my_map_code;
    }
    QString sql_seat = "district_name";
    QString dbtablename_seat = "tb_district_seats";
    QString id = QString("district_type = %1 and map_code = '%2'").arg(type).arg(map_code);
    QList<QStringList> dataList_seat = MyDatabase::getInstance().query_some_tabledata(sql_seat,dbtablename_seat,id);
    if(dataList_seat.isEmpty())
    {
        return;
    }
    int rowcount_seat = dataList_seat.count();

    {
        if(boxName == "comboBox_start_district_type")
        {
            ui->comboBox_start_district_name->blockSignals(true);
            ui->comboBox_start_district_name->clear();
            ui->comboBox_start_district_name->blockSignals(false);
            for (int row = 0; row < rowcount_seat; row++) {
                if(ui->comboBox_start_district_name->findText(dataList_seat[row][0])==-1)
                {
                    ui->comboBox_start_district_name->addItem(dataList_seat[row][0]);
                }

            }
        }
        else if(boxName == "comboBox_end_district_type"){
            ui->comboBox_end_district_name->blockSignals(true);
            ui->comboBox_end_district_name->clear();
            ui->comboBox_end_district_name->blockSignals(false);
            for (int row = 0; row < rowcount_seat; row++) {
                if(ui->comboBox_end_district_name->findText(dataList_seat[row][0])==-1)
                {
                    ui->comboBox_end_district_name->addItem(dataList_seat[row][0]);
                }

            }
        }
        else if(boxName == "comboBox_start_district_type_s")
        {
            ui->comboBox_start_district_name_s->blockSignals(true);
            ui->comboBox_start_district_name_s->clear();
            ui->comboBox_start_district_name_s->blockSignals(false);
            for (int row = 0; row < rowcount_seat; row++) {
                if(ui->comboBox_start_district_name_s->findText(dataList_seat[row][0])==-1)
                {
                    ui->comboBox_start_district_name_s->addItem(dataList_seat[row][0]);
                }

            }
        }
    }
}

/**
 * @brief  指定座位名下拉框选项
 */
void AGV_Send_TaskDialog::seattoBoxData(const QString& text)
{
    if(text.isEmpty())
    {
        return;
    }
    QComboBox *comboBox = qobject_cast<QComboBox*>(sender());
    if(!comboBox)
    {
        return;
    }
    QString boxName = comboBox->objectName();
    QString type = ui->comboBox_start_district_type->currentData().toString();
    if(boxName == "comboBox_end_district_name")
    {
        type = ui->comboBox_end_district_type->currentData().toString();
    }
    if(boxName == "comboBox_start_district_name_s")
    {
        type = ui->comboBox_start_district_type_s->currentData().toString();
    }
    QString name = comboBox->currentText();
    QString sql_seat = "seat";
    QString dbtablename_seat = "tb_district_seats";
    QString id = QString("district_type = %1 and district_name = '%2'").arg(type).arg(name);
    QList<QStringList> dataList_seat = MyDatabase::getInstance().query_some_tabledata(sql_seat,dbtablename_seat,id);
    if(dataList_seat.isEmpty())
    {
        return;
    }
    int rowcount_seat = dataList_seat.count();
    {
        if(boxName == "comboBox_start_district_name")
        {
            ui->comboBox_start_seat->blockSignals(true);
            ui->comboBox_start_seat->clear();
            ui->comboBox_start_seat->blockSignals(false);
            for (int row = 0; row < rowcount_seat; row++) {
                if(ui->comboBox_start_seat->findText(dataList_seat[row][0])==-1)
                {
                    ui->comboBox_start_seat->addItem(dataList_seat[row][0]);
                }
            }
        }
        else if(boxName == "comboBox_end_district_name")
        {
            ui->comboBox_end_seat->blockSignals(true);
            ui->comboBox_end_seat->clear();
            ui->comboBox_end_seat->blockSignals(false);
            for (int row = 0; row < rowcount_seat; row++) {
                if(ui->comboBox_end_seat->findText(dataList_seat[row][0])==-1)
                {
                    ui->comboBox_end_seat->addItem(dataList_seat[row][0]);
                }
            }
        }
        else if(boxName == "comboBox_start_district_name_s")
        {
            ui->comboBox_start_seat_s->blockSignals(true);
            ui->comboBox_start_seat_s->clear();
            ui->comboBox_start_seat_s->blockSignals(false);
            for (int row = 0; row < rowcount_seat; row++) {
                if(ui->comboBox_start_seat_s->findText(dataList_seat[row][0])==-1)
                {
                    ui->comboBox_start_seat_s->addItem(dataList_seat[row][0]);
                }
            }
        }
    }
}

/**
 * @brief  笼车任务信息协议发送
 */
void AGV_Send_TaskDialog::on_pushButton_transfer_trolley_clicked()
{
    // 暂停tb_tasks_error数据实时刷新的定时器
    MyDatabase::getInstance().stopTimer("tb_tasks_error");

    //发送数据类
    QMap<QString, QVariant> infomap;
    QMap<QString, QVariant> dataMap;

    QString district_start_name = ui->comboBox_start_district_name_s->currentText();
    QString district_start_type = ui->comboBox_start_district_type_s->currentData().toString();
    QString seat_start = ui->comboBox_start_seat_s->currentText();

    QString district_end_name;
    QString district_end_type;
    QString seat_end;

    // 设置主任务结束区域
    QString source_task =  ui->comboBox_source_task_id->currentText();
    QString sql_end = "is_parent_task,goal_node_id";
    QString dbtablename_end = "tb_tasks";
    QString sead_end = QString("source_task_id = '%1'").arg(source_task);
    QList<QStringList> dataList_end = MyDatabase::getInstance().query_some_tabledata(sql_end,dbtablename_end,sead_end);
    int rowcount_seat = dataList_end.count();
    for (int row = 0; row < rowcount_seat; row++) {
        if(dataList_end[row][0] == "1")
        {
            QString sql = "district_name,district_type,seat";
            QString dbtablename = "tb_district_seats";
            QString sead = QString("node_id = '%1'").arg(dataList_end[row][1]);
            QList<QStringList> dataList = MyDatabase::getInstance().query_some_tabledata(sql,dbtablename,sead);
            if(dataList.count() > 0)
            {
                district_end_name = dataList[0][0];
                district_end_type = dataList[0][1];
                seat_end = dataList[0][2];
            }

        }
    }

    QString start_region = QString("%1%2%3").arg(district_start_name).arg(district_start_type).arg(seat_start);
    QString end_region = QString("%1%2%3").arg(district_end_name).arg(district_end_type).arg(seat_end);
    QDateTime timer = QDateTime::currentDateTime();

    infomap["source_task_id"] = ui->comboBox_source_task_id->currentText();
    infomap["carNo"] = ui->comboBox_transfer_trolley_id->currentText().toInt();
    infomap["action_group_id"] = ui->comboBox_action_group_transfer_trolley->currentData().toInt();
    infomap["start_region"] = start_region;
    infomap["end_region"] = end_region;
    infomap["create_time"] = timer.toString("yyyy-MM-dd hh:mm:ss.zzz");
    infomap["priority"] = ui->comboBox_priority_s->currentData().toInt();

    if(CommandSendorReceive::getInstance().isExecute("提示","确认发送任务接替指令"))
    {
        //打包成json数据结构
        CommandSendorReceive::getInstance().sendCommand(dataMap, infomap, 104, false);
        LOG_INFO(QString("笼车%1任务接替").arg(ui->comboBox_transfer_trolley_id->currentText()));
    }

    // 启动tb_tasks_error数据刷新定时器
    MyDatabase::getInstance().startTimer("tb_tasks_error");
    //关闭窗口
    close();
}

/**
 * @brief  AGV任务信息协议发送
 */
void AGV_Send_TaskDialog::on_pushButton_task_clicked()
{
    //发送数据类
    QMap<QString, QVariant> infomap;
    QMap<QString, QVariant> dataMap;
//    QString sql_seat = "district_name,district_type,seat";
    QString district_start_name = ui->comboBox_start_district_name->currentText();
    QString district_end_name = ui->comboBox_end_district_name->currentText();
    QString district_start_type = ui->comboBox_start_district_type->currentData().toString();
    QString district_end_type = ui->comboBox_end_district_type->currentData().toString();
    QString seat_start = ui->comboBox_start_seat->currentText();
    QString seat_end = ui->comboBox_end_seat->currentText();
    QString start_region = "";
    QString end_region = "";
    if(seat_start != "")
    {
        start_region = QString("%1%2%3").arg(district_start_name).arg(district_start_type).arg(seat_start);
    }
    if(seat_end != "")
    {
        end_region = QString("%1%2%3").arg(district_end_name).arg(district_end_type).arg(seat_end);
    }
    QDateTime timer = QDateTime::currentDateTime();

    infomap["isSpecificVehicle"] = 0;
    infomap["vehicle_id"] = -99;
    if(isSpecificVehicle)
    {
        infomap["isSpecificVehicle"] = 1;
        infomap["vehicle_id"] = ui->comboBox_vehicle->currentData().toInt();
    }
    infomap["action_group_id"] = ui->comboBox_action_group->currentData().toInt();
    infomap["start_region"] = start_region;
    infomap["end_region"] = end_region;
    infomap["create_time"] = timer.toString("yyyy-MM-dd hh:mm:ss.zzz");
    infomap["priority"] = ui->comboBox_priority->currentData().toInt();
    if(CommandSendorReceive::getInstance().isExecute("提示","确认发送任务下发指令"))
    {
        //打包成json数据结构
        CommandSendorReceive::getInstance().sendCommand(dataMap, infomap, 101, false);
        LOG_INFO("任务指令下发");
    }
    //关闭窗口
    close();
}




